package com.atguigu.upp.service;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.spark.sql.SparkSession;

import java.io.IOException;
import java.io.InputStream;
import java.util.ResourceBundle;

/**
 * Created by Smexy on 2023/1/3
 *      封装一些工具方法
 */
public class UPPUtil
{
    public static SqlSessionFactory getSqlSessionFactoryByConfig(String config) throws IOException {

        InputStream inputStream = Resources.getResourceAsStream(config);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory;

    }

    //创建一个SparkSession
    public static SparkSession getSparkSession(String appName){

        SparkSession sparkSession =SparkSession.builder()
                                                   .appName(appName)
                                                   .enableHiveSupport()
                                                   // 使用的集群
                                                   .master(getPropertyValue("masterUrl"))
                                                   .getOrCreate();

        return sparkSession;

    }

    //看作是一个Map集合
    static ResourceBundle resourceBundle = ResourceBundle.getBundle("config");

    public static String getPropertyValue(String key){

        return resourceBundle.getString(key);

    }

    public static void main(String[] args) {

        System.out.println(getPropertyValue("masterUrl"));

    }

}
